from flask import Flask, request, jsonify, send_from_directory
import requests
import time
import os

app = Flask(__name__, static_folder=".")

# 提供静态文件
@app.route('/')
def index():
    return send_from_directory('.', 'index.html')

# 提供其他静态文件
@app.route('/<path:path>')
def static_files(path):
    return send_from_directory('.', path)

# 代理接口
@app.route('/test-response')
def test_response():
    url = request.args.get('url')
    
    if not url:
        return jsonify({"error": "请提供URL参数"}), 400
    
    try:
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
        }
        
        start_time = time.time()
        response = requests.get(url, headers=headers, timeout=10, verify=True)
        end_time = time.time()
        
        response_time = int((end_time - start_time) * 1000)  # 转换为毫秒
        
        return jsonify({
            "responseTime": response_time,
            "status": response.status_code,
            "size": len(response.content),
            "headers": dict(response.headers)
        })
    
    except Exception as e:
        return jsonify({
            "error": str(e),
            "responseTime": 0
        }), 500

if __name__ == '__main__':
    port = int(os.environ.get('PORT', 5000))
    print(f"服务器运行在 http://localhost:{port}")
    print("现在可以通过浏览器访问此地址测试网页响应时间")
    app.run(host='0.0.0.0', port=port) 